package com.sinog.front.controller.biz;

import com.sinog.core.model.UserDTO;
import com.sinog.core.util.JsonUtil;
import com.sinog.core.utilbean.Constants;
import com.sinog.core.utilbean.Result;
import com.sinog.front.config.idempotent.Idempotent;
import com.sinog.front.config.log.Log;
import com.sinog.front.feign.biz.SocialRelationsService;
import com.sinog.front.service.user.UserFacade;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

/**
 * @desc 预安置管理 - 服刑人员信息录入
 * @author hak
 * @date 2020年1月15日
 */
@RestController
public class SocialRelationsController {

    @Resource
    private SocialRelationsService socialRelationsService;
    @Resource
    private UserFacade userService;

    /**
     * 预安置人员信息录入 - 保存/更新 未成年子女信息
     * @param request request
     * @param data data
     * @return Result
     */
    @Log(type = "更新", value = "保存未成年子女信息")
    @Idempotent(paramPos = 1)
    @PostMapping("/azbj/social/noadult/save")
    public Result saveNoAdult(HttpServletRequest request,
                              @RequestParam(required = false, defaultValue = "null") String data) {
        UserDTO user = userService.getAuthentication();
        String id = JsonUtil.getValueFromJsonstrByKey(data, Constants.StrFinalValue.ID);
        if(StringUtils.isNotBlank(id)) {
            return socialRelationsService.updateNoAdult(data,user);
        }
        return socialRelationsService.saveNoAdult(data,user);
    }

    /**
     * 预安置人员信息录入 - 保存/更新 未成年子女信息excel
     * @param request request
     * @param data data
     * @return Result
     */
    @Log(type = "更新", value = "保存未成年子女信息")
    @Idempotent(paramPos = 1)
    @PostMapping("/azbj/social/noadult/saveexcel")
    public Result saveNoAdultExcel(HttpServletRequest request,
                                   @RequestParam(required = false, defaultValue = "null") String data) {
        UserDTO user = userService.getAuthentication();
        String id = JsonUtil.getValueFromJsonstrByKey(data, Constants.StrFinalValue.ID);
        if(StringUtils.isNotBlank(id)) {
            return socialRelationsService.updateNoAdultExcel(data,user);
        }
        return socialRelationsService.saveNoAdult(data,user);
    }

    /**
     * 预安置人员信息录入 - 获取未成年子女列表信息
     * @param pageIndex pageIndex
     * @param pageSize pageSize
     * @param querykey querykey
     * @return String
     */
    @RequestMapping("/azbj/noadult/list")
    public String getNoAdultList(Integer pageIndex,Integer pageSize,
                                 @RequestParam(required = false, defaultValue = "-1") String querykey) {
        return socialRelationsService.getNoAdultList(pageIndex,pageSize,querykey);
    }

    /**
     * 预安置人员信息录入 - 获取未成年子女列表信息excel
     * @param pageIndex pageIndex
     * @param pageSize pageSize
     * @param querykey querykey
     * @return String
     */
    @RequestMapping("/azbj/noadult/listexcel")
    public String getNoAdultListExcel(Integer pageIndex,Integer pageSize,
                                      @RequestParam(required = false, defaultValue = "-1") String querykey) {
        return socialRelationsService.getNoAdultListExcel(pageIndex,pageSize,querykey);
    }

    /**
     * 根据id获取未成年子女的信息
     * @param id id
     * @return String
     */
    @PostMapping("/azbj/social/getInfo")
    public String getInfoById(@RequestParam("id") String id) {
        return socialRelationsService.getInfoById(id);
    }

    /**
     * 根据id获取未成年子女的信息excel
     * @param id id
     * @return String
     */
    @PostMapping("/azbj/social/getInfoexcel")
    public String getInfoByIdExcel(@RequestParam("id") String id) {
        return socialRelationsService.getInfoByIdExcel(id);
    }

    /**
     * 删除未成年子女信息（逻辑删除）
     * @param request request
     * @param id id
     * @return Result
     */
    @Log(type = "删除", value = "删除未成年子女信息")
    @PostMapping("/azbj/social/noadultremove")
    public Result deleteNoAultById(HttpServletRequest request,@RequestParam("id") String id) {
        return socialRelationsService.deleteNoAultById(id);
    }

    /**
     * 删除未成年子女信息（逻辑删除）excel
     * @param request request
     * @param id id
     * @return Result
     */
    @Log(type = "删除", value = "删除未成年子女信息")
    @PostMapping("/azbj/social/noadultremoveexcel")
    public Result deleteNoAultByIdExcel(HttpServletRequest request,@RequestParam("id") String id) {
        return socialRelationsService.deleteNoAultByIdExcel(id);
    }

    /**
     * 预安置人员信息录入 - 社会关系 - 保存或者更新社会关系信息
     * @param request request
     * @param data data
     * @return Result
     */
    @Log(type = "更新", value = "保存社会关系信息")
    @Idempotent(paramPos = 1)
    @PostMapping("/azbj/social/srinfo/save")
    public Result addSrInfo(HttpServletRequest request,
                            @RequestParam(required = false, defaultValue = "-1") String data) {
        UserDTO user = userService.getAuthentication();
        String id = JsonUtil.getValueFromJsonstrByKey(data, Constants.StrFinalValue.ID);
        if(StringUtils.isNotBlank(id)) {
            return socialRelationsService.updataSrInfo(data,user);
        }
        return socialRelationsService.saveSrInfo(data,user);
    }

    /**
     * 预安置人员信息录入 - 社会关系 - 保存或者更新社会关系信息excel
     * @param request request
     * @param data data
     * @return Result
     */
    @Log(type = "更新", value = "保存社会关系信息")
    @Idempotent(paramPos = 1)
    @PostMapping("/azbj/social/srinfo/saveexcel")
    public Result addSrInfoExcel(HttpServletRequest request,
                                 @RequestParam(required = false, defaultValue = "-1") String data) {
        UserDTO user = userService.getAuthentication();
        String id = JsonUtil.getValueFromJsonstrByKey(data, Constants.StrFinalValue.ID);
        if(StringUtils.isNotBlank(id)) {
            return socialRelationsService.updataSrInfoExcel(data,user);
        }
        return socialRelationsService.saveSrInfo(data,user);
    }

    /**
     * 预安置人员信息录入 - 社会关系 - 配偶、父母或其他 - 列表
     * @param pageIndex pageIndex
     * @param pageSize pageSize
     * @param priNum priNum
     * @return String
     */
    @PostMapping("/azbj/social/srlist")
    public String getSrList(Integer pageIndex,Integer pageSize,
                            @RequestParam(required = false, defaultValue = "-1") String priNum) {
        return socialRelationsService.getSrList(pageIndex,pageSize,priNum);
    }

    /**
     * 预安置人员信息录入 - 社会关系 - 配偶、父母或其他 - 列表excel
     * @param pageIndex pageIndex
     * @param pageSize pageSize
     * @param priNum priNum
     * @return String
     */
    @PostMapping("/azbj/social/srlistexcel")
    public String getSrListExcel(Integer pageIndex,Integer pageSize,
                                 @RequestParam(required = false, defaultValue = "-1") String priNum) {
        return socialRelationsService.getSrListExcel(pageIndex,pageSize,priNum);
    }

    /**
     * 根据ID查询社会关系 - 配偶、父母或其他信息
     * @param id id
     * @return String
     */
    @PostMapping("/azbj/social/srinfo")
    public String getSrInfoById(@RequestParam(required = false, defaultValue = "-1") String id) {
        return socialRelationsService.getSrInfoById(id);
    }

    /**
     * 根据ID查询社会关系 - 配偶、父母或其他信息excel
     * @param id id
     * @return String
     */
    @PostMapping("/azbj/social/srinfoexcel")
    public String getSrInfoByIdExcel(@RequestParam(required = false, defaultValue = "-1") String id) {
        return socialRelationsService.getSrInfoByIdExcel(id);
    }

    /**
     * 删除社会关系信息（逻辑删除）
     * @param request request
     * @param id id
     * @param priNum priNum
     * @return Result
     */
    @Log(type = "删除", value = "删除社会关系信息")
    @PostMapping("/azbj/social/srinforemove")
    public Result deleteSrInfo(HttpServletRequest request,
                               @RequestParam(required = false, defaultValue = "-1") String id,
                               @RequestParam(required = false, defaultValue = "-1") String priNum) {
        return socialRelationsService.deleteSrInfo(id,priNum);
    }

    /**
     * 删除社会关系信息（逻辑删除）excel
     * @param request request
     * @param id id
     * @param priNum priNum
     * @return Result
     */
    @Log(type = "删除", value = "删除社会关系信息")
    @PostMapping("/azbj/social/srinforemoveexcel")
    public Result deleteSrInfoExcel(HttpServletRequest request,
                                    @RequestParam(required = false, defaultValue = "-1") String id,
                                    @RequestParam(required = false, defaultValue = "-1") String priNum) {
        return socialRelationsService.deleteSrInfoExcel(id,priNum);
    }
}